/*
 * AvionDAOImpl.java
 *
 * Created on 23 de noviembre de 2007, 19:02
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.fiuba.aterrizar.servidor.dal.dao.impl;

import com.fiuba.aterrizar.servidor.conf.ConnectionFactory;
import com.fiuba.aterrizar.servidor.dal.dao.AvionDAO;
import com.fiuba.aterrizar.servidor.domain.Avion;
import com.fiuba.aterrizar.servidor.domain.ModeloAvion;
import com.fiuba.aterrizar.servidor.domain.Reserva;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author Demian
 */
public class AvionDAOImpl implements AvionDAO{
        
    public Collection findAll(String modelo, String codigo){        
        Collection aviones = new ArrayList();
        Connection conn = null;
        ConnectionFactory factory = ConnectionFactory.getInstance();
        PreparedStatement stm = null;
        try {
            conn = factory.getConnection();            
            String query = "Select A.codigo,A.modelo,A.estado,A.cantidadAsientosEjecutiva," +
                    "A.cantidadAsientosPrimera,A.cantidadAsientosTurista,B.descripcion clase FROM avion A, modelosAviones B WHERE (modelo = ? or ? is null)" +
                    " AND (codigo = ? OR ? is null) AND A.modelo = B.id";
                     
            stm = conn.prepareStatement(query);               
            stm.setString(1,modelo);
            stm.setString(2,modelo);
            stm.setString(3,codigo);
            stm.setString(4,codigo);            
            
            ResultSet rs = stm.executeQuery();
            while (rs.next()){                                
                Avion avion = new Avion();
                avion.setCodigo(rs.getString(1));
                avion.setModelo(rs.getString(2));
                avion.setEstado(rs.getString(3));
                avion.setCantidadAsientosEjecutiva(rs.getString(4));
                avion.setCantidadAsientosPrimera(rs.getString(5));
                avion.setCantidadAsientosTurista(rs.getString(6));
                avion.setDescripcionModelo(rs.getString(7));
                
                aviones.add(avion);
            }
            
            return aviones;
        } catch (SQLException ex) {
            throw new RuntimeException("Error al buscar la reserva",ex);
        } finally {
            factory.closeStatement(stm);
            factory.closeConnection(conn);
        }
    }   

    public Collection findAllModelos() {
        Collection modelos = new ArrayList();
        Connection conn = null;
        ConnectionFactory factory = ConnectionFactory.getInstance();
        PreparedStatement stm = null;
        try {
            conn = factory.getConnection();            
            stm = conn.prepareStatement("Select id, descripcion FROM modelosaviones");
                        
            ResultSet rs = stm.executeQuery();
            while (rs.next()){                                
                ModeloAvion modelo = new ModeloAvion();
                modelo.setCodigo(rs.getString(1));
                modelo.setDescripcion(rs.getString(2));
                modelos.add(modelo);
            }
            
            return modelos;
        } catch (SQLException ex) {
            throw new RuntimeException("Error al buscar el vuelo",ex);
        } finally {
            factory.closeStatement(stm);
            factory.closeConnection(conn);
        }
    }

    public void delete(String codigo) {        
        Connection conn = null;
        ConnectionFactory factory = ConnectionFactory.getInstance();
        PreparedStatement stm = null;
        try {
            conn = factory.getConnection();            
            stm = conn.prepareStatement("DELETE from avion WHERE codigo = ?");
            stm.setString(1,codigo);
            
            stm.execute();            
        } catch (SQLException ex) {
            throw new RuntimeException("Error al borrar el vuelo",ex);
        } finally {
            factory.closeStatement(stm);
            factory.closeConnection(conn);
        }        
    }

    public void save(Avion avion) {
        Connection conn = null;
        ConnectionFactory factory = ConnectionFactory.getInstance();
        PreparedStatement stm = null;
        try {
            conn = factory.getConnection();            
            stm = conn.prepareStatement("INSERT INTO Avion (codigo,estado,modelo,cantidadAsientosPrimera," +
                    "cantidadAsientosTurista,cantidadAsientosEjecutiva) values (?,?,?,?,?,?)");
            stm.setString(1,avion.getCodigo());
            stm.setString(2,avion.getEstado());
            stm.setString(3,avion.getModelo());
            stm.setString(4,avion.getCantidadAsientosPrimera());
            stm.setString(5,avion.getCantidadAsientosTurista());
            stm.setString(6,avion.getCantidadAsientosEjecutiva());
            
            stm.execute();            
        } catch (SQLException ex) {
            throw new RuntimeException("Error al guardar el avion",ex);
        } finally {
            factory.closeStatement(stm);
            factory.closeConnection(conn);
        }
    }

    public void update(Avion avion) {
        Connection conn = null;
        ConnectionFactory factory = ConnectionFactory.getInstance();
        PreparedStatement stm = null;
        try {
            conn = factory.getConnection();            
            stm = conn.prepareStatement("UPDATE avion SET estado=?,modelo=?,cantidadAsientosPrimera=?," +
                    "cantidadAsientosTurista=?,cantidadAsientosEjecutiva=? WHERE codigo=?");
            
            stm.setString(1,avion.getEstado());
            stm.setString(2,avion.getModelo());
            stm.setString(3,avion.getCantidadAsientosPrimera());
            stm.setString(4,avion.getCantidadAsientosTurista());
            stm.setString(5,avion.getCantidadAsientosEjecutiva());
            stm.setString(6,avion.getCodigo());
                        
            stm.execute();            
        } catch (SQLException ex) {
            throw new RuntimeException("Error hacer el update del avion",ex);
        } finally {
            factory.closeStatement(stm);
            factory.closeConnection(conn);
        }
    }
    
}
